﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using PaperSystem.Models;

namespace PaperSystem.Models
{
    public class StatisticsRepository
    {
        PaperDatabaseEntities db;

        public StatisticsRepository()
        {
            db = new PaperDatabaseEntities();
        }

        //根据信息获取题目显示列表
        public List<StatisticsQuestionList> GetQuestionList(QuestionQuery info)
        {
            var list = (from a in db.Ex_question
                        select new StatisticsQuestionList()
                        {
                            content = a.Econtent,
                            course = a.Course.Cname,
                            creator = a.Teacher.Tname,
                            eid = a.Eid,
                            type = a.Etype,
                        });
            if (info != null)
            {
                if (info.course != null)
                {
                    list = list.Where(a => a.course == info.course);
                }
                if (info.type != null)
                {
                    list = list.Where(a => a.type == info.type);
                }
                if (info.creator != null)
                {
                    list = list.Where(a => a.creator.Contains(info.creator));
                }
            }

            List<StatisticsQuestionList> list2 = list.ToList();

            //每道大题的总分值
            double exPoint = 0;
            //每道大题的学生总得分
            double exScore = 0;
            //double一个rate
            double rate = 0;


            foreach (var temp in list2)
            {
                exPoint = 0;
                exScore = 0;

                var points = db.Paper_object.Where(a => a.In_question.Ex_question.Eid == temp.eid).Select(a => a.Point);
                foreach (var point in points)
                {
                    exPoint += point.Value;
                }

                var exam = db.Exam_Answer.Where(a => a.Paper_object.In_question.Ex_question.Eid == temp.eid);

                var scores = exam.Select(a => a.Score);
                foreach (var score in scores)
                {
                    exScore += (score == null) ? 0 : score.Value;
                }

                temp.stuNum = exam.Select(a => a.Sid).Distinct().Count();
                rate = (!exPoint.Equals(0) && !temp.stuNum.Equals(0)) ? (exScore / exPoint / temp.stuNum) : 0;
                temp.rate = rate.ToString("P");
            }
            /*
             * 遍历大题
             *      遍历小题
             *          每一小题
             *          1.根据eid,POorder查询考试答案表
             *            取出符合条件的行的得分，累加
             *            count大题人数，累加
             *          2.根据eid,POorder查询试卷内容表
             *            取出每一小题的分值，累加
             * 每一大题都得到3个值：大题的总分值、大题的总得分值、答题人数
             */
            return list2;
        }

        //根据教师id返回用户名
        public string TeacherName(string id)
        {
            string username = "";
            try
            {            
                var tempdata = db.Teacher.Where(t => t.Tid == id).FirstOrDefault();
                username = tempdata.Tname;
            }
            catch (Exception e)
            {
            }

            return username;
        }

        //返回所有课程名称
        public SelectList GetCourse()
        {
            var list = db.Course.ToList();
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (var a in list)
            {
                listItem.Add(new SelectListItem { Text = a.Cname, Value = a.Cname });
            }
            return new SelectList(listItem, "Value", "Text", "");
        }

        //返回所有课程名称，有默认选中值
        public SelectList GetCourse(string selected)
        {
            var list = db.Course.ToList();
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (var a in list)
            {
                listItem.Add(new SelectListItem { Text = a.Cname, Value = a.Cname });
            }
            return new SelectList(listItem, "Value", "Text", selected);
        }

        //创建题目类型下拉框的内容
        public SelectList GetType()
        {
            //TODO：内容待定
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "阅读理解", Value = "阅读理解" });
            listItem.Add(new SelectListItem { Text = "完形填空", Value = "完形填空" });
            listItem.Add(new SelectListItem { Text = "翻译", Value = "翻译" });
            listItem.Add(new SelectListItem { Text = "作文", Value = "作文" });
            listItem.Add(new SelectListItem { Text = "综合选择", Value = "综合选择" });
            SelectList list = new SelectList(listItem, "Value", "Text");
            return list;
        }

        //创建题目类型下拉框的内容，有默认选中值
        public SelectList GetType(string selected)
        {
            //TODO：内容待定
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "阅读理解", Value = "阅读理解" });
            listItem.Add(new SelectListItem { Text = "完形填空", Value = "完形填空" });
            listItem.Add(new SelectListItem { Text = "翻译", Value = "翻译" });
            listItem.Add(new SelectListItem { Text = "作文", Value = "作文" });
            listItem.Add(new SelectListItem { Text = "综合选择", Value = "综合选择" });
            SelectList list = new SelectList(listItem, "Value", "Text", selected);
            return list;
        }

        /*===================以上是从QuestionRepository复制进来的 ===========*/

        public List<StatisticsPaperList> GetPaperList(PaperQuery info)
        {
            //每张试卷的总分值
            double exPoint = 0;
            //每张试卷的学生总得分
            double exScore = 0;
            //double一个rate
            double rate = 0;

            var list = (from a in db.Paper
                        where a.TorE == "考试"
                        select new StatisticsPaperList
                        {
                            course = a.Course.Cname,
                            creator = a.Teacher.Tname,
                            paperTitle = a.Title,
                            pid = a.Pid
                        });

            if (info.course != null)
            {
                list = list.Where(a => a.course == info.course);
            }
            if (info.creator != null)
            {
                list = list.Where(a => a.creator.Contains(info.creator));
            }
            if (info.paperTitle != null)
            {
                list = list.Where(a => a.paperTitle.Contains(info.paperTitle));
            }

            List<StatisticsPaperList> list2 = list.ToList();
            foreach (var temp in list2)
            {
                exPoint = 0;
                exScore = 0;

                temp.strNum = db.Exam_Answer.Where(a => a.Pid == temp.pid).Select(a => a.Sid).Distinct().Count();

                var paperObjects = db.Paper_object.Where(a => a.Pid == temp.pid);
                var points = paperObjects.Select(a => a.Point);
                foreach (var point in points)
                {
                    exPoint += point.Value;
                }

                var exam = db.Exam_Answer.Where(a => a.Paper_object.Pid == temp.pid);

                var scores = exam.Select(a => a.Score);
                foreach (var score in scores)
                {
                    exScore += (score == null) ? 0 : score.Value;
                }

                if (!exPoint.Equals(0) && !temp.strNum.Equals(0))
                {
                    rate = exScore / exPoint / temp.strNum;
                }
                else
                {
                    rate = 0;
                }
                temp.rate = rate.ToString("P");

                /*var everyoneEachTimeScores = exam.GroupBy(a => a.Sid).Select();
                                            
                //得出一个数组/列表：每个人每次考试得到的分数eachScore
                if (eachScore < 0.6 * exPoint)
                {
                }
                else if (eachScore < 0.7 * exPoint)
                {
                }*/
            }
            return list2;
        }


    }
}
